@extends('admin.common.main')
@section('css')
    {{--webuploader样式--}}
    <link rel="stylesheet" type="text/css" href="/webuploader/webuploader.css" />

@endsection
@section('cnt')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 用户中心 <span class="c-gray en">&gt;</span> 添加用户 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>

    <article class="page-container">
        @include('admin.common.validate')
        <form action="{{ route('admin.fang.store') }}" method="post" class="form form-horizontal" id="fang-add">
            @csrf
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>房源名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="fang_name">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>小区名称：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="fang_xiaoqu">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>小区地址：</label>
                <div class="formControls col-xs-4 col-sm-4">
                    <select name="fang_province" style="width: 100px;" onchange="selectCity(this,'fang_city')">
                        <option value="0">==请选择省==</option>
                        @foreach($cityData as $item)
                            <option value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                    <select name="fang_city" id="fang_city" style="width: 100px;" onchange="selectCity(this,'fang_region')">
                        <option value="0">==市==</option>
                    </select>
                    <select name="fang_region" id="fang_region" style="width: 100px;">
                        <option value="0">==区/县==</option>
                    </select>
                </div>
                <div class="formControls col-xs-4 col-sm-5">
                    <input type="text" class="input-text" name="fang_addr" placeholder="小区详情地址和房源说明">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>租金：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" style="width: 200px;" name="fang_rent"> 元
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>楼层：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" style="width: 200px;" name="fang_floor">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>付款方式：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_region" style="width: 200px;">
                        @foreach($fang_rent_type_data as $item)
                            <option value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>几室：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" name="fang_shi" value="1">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>几厅：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" name="fang_ting" value="1">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>几卫：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" name="fang_wei" value="1">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>朝向：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_direction" style="width: 200px;">
                        @foreach($fang_direction_data as $item)
                            <option value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>租赁方式：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_rent_class" style="width: 200px;">
                        @foreach($fang_rent_class_data as $item)
                            <option value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>建筑面积：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" name="fang_build_area" value="60" style="width: 60px;">平米
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>使用面积：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="number" class="input-text" name="fang_using_area" value="40" style="width: 60px;">平米
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>建筑年代：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" onfocus="WdatePicker({dateFmt:'yyyy'})" name="fang_year" class="input-text Wdate" style="width:120px;">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>配套设施：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    @foreach($fang_config_data as $item)
                        <label>
                            <input type="checkbox" name="fang_config[]" value="{{ $item->id }}"/>
                            {{ $item->name }} &nbsp;&nbsp;
                        </label>
                    @endforeach
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>房屋图片：</label>
                <div class="formControls col-xs-2 col-sm-2">
                    <div id="picker">房屋图片</div>
                </div>
                <div class="formControls col-xs-6 col-sm-7">
                    <!-- 表单提交时，上传图片地址，以#隔开 -->
                    <input type="hidden" name="fang_pic" id="fang_pic"/>
                    <!-- 显示上传成功后的图片容器 -->
                    <div id="imglist"></div>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>房东：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="fang_owner" style="width: 200px;">
                        @foreach($ownerData as $item)
                            <option value="{{ $item->id }}">{{ $item->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否推荐：</label>
                <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                    <div class="radio-box">
                        <label>
                            <input name="is_recommend" type="radio" value="0" checked>
                            否
                        </label>
                    </div>
                    <div class="radio-box">
                        <label>
                            <input type="radio" value="1" name="is_recommend">
                            是
                        </label>
                    </div>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>房屋描述：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <textarea name="fang_desn" class="form-control textarea"></textarea>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>房屋详情：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <textarea id="fang_body" name="fang_body">房屋详情信息添加</textarea>
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="添加房源">
                </div>
            </div>
        </form>
    </article>
    @endsection
@section('js')
    <script src="/webuploader/webuploader.js"></script>

    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
    <script>
        function selectCity(obj,selectName) {
            //得到选中的省份
            let value = $(obj).val();
            //发起ajax 以省份id得到市
            $.get('{{route('admin.fang.city')}}',{id:value}).then(jsonArr=>{
                let html=`<option value="0">==市==</option>`;
                jsonArr.map(item=>{
                    var {id,name} = item;
                    html +=`<option value="${id}">${name}</option>`;
                });
                $('#' + selectName).html(html);
            });
        }
        //单选框样式
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
        //前端表单验证
        // $('#form-member-add').validate({
        //     rules:{
        //         name:{
        //             required:true
        //         },
        //         age:{
        //             required:true
        //         },
        //         card:{
        //             required:true
        //         },
        //         email:{
        //             email:true
        //         },
        //         phone:{
        //             phone:true
        //         }
        //     },
        //     massage:{
        //         name: {
        //            required: '真实名字要写',
        //         },
        //     },
        //     onkeyup:false, //取消键盘时间
        //     // focusCleanup:true,
        //     success:"valid", //验证成功样式
        //     //验证通过后，处理的方法，form dom对象
        //     submitHandler:function(form){
        //         //表单提交
        //        form.submit();
        //     }
        // });
        //自定义验证规则
        // 手机验证
        jQuery.validator.addMethod("phone", function(value, element) {
            var reg1 = /^\+86-1[3-9]\d{9}$/;
            var reg2 = /^1[3-9]\d{9}$/;
            var ret = reg1.test(value) || reg2.test(value)
;            return this.optional(element) || ret;
        }, "请输入正确的手机号");

        var uploader = WebUploader.create({
            //选完文件自动上传
            auto:true,
            // swf文件路径
            swf: '/webuploader/js/Uploader.swf',

            // 文件接收服务端。
            server: '{{route('admin.fang.upfile')}}',
            //文件参数
            formData:{
                _token: '{{csrf_token()}}'
            },
            //文件上传表单名称
            fileVal:'file',

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick:{
                id:'#picker',
                //是否开启选择多文件能力
                multiple:true,
            } ,

            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false
        });
        // 文件上传成功，给item添加成功class, 用样式标记上传成功。回调方法
        uploader.on( 'uploadSuccess', function( file ,ret) {
            //解决表单提交时，图片#隔开
            let val = $('#pic').val();
            let tmp = val + '#' +ret.url;
            $('#pic').val(tmp);
            //图片显示
            let imglist = $('#imglist');
            let html = `<div style="width: 100px;position: relative">
                <img src="${ret.url}" style="width: 100px">
                <strong onclick="delpic(this,'${ret.url}')" style="     position: absolute;
             right: 2px;
             top: 2px;color:red;font-size: 20px">X</strong>
                </div>`;
            //一定要用追加而不是html覆盖
             imglist.append(html)
            // let src = ret.url;
            //
            // //给表单添加value值
            // $("#icon").val(src);
            // //给图片添加
            // $('#pic').attr('src',src);
        });
        //删除图片
        function delpic(obj,picurl) {
            let url = "{{route('admin.fang.delfile')}}?file="+picurl;
            //发送请求
            fetch(url);
            //删除当前点击的图片显示
            $(obj).parent().remove();
            //修改隐藏域表单
            $('#pic').val($('#pic').val().replace(`#${picurl}`,''));


        }

    </script>

    @endsection